package model;

import java.util.Date;

import model.type.SubjectAttributeType;
import model.type.SubjectType;
import db.ImbiConnection;

public class SubjectAttribute implements Comparable<SubjectAttribute>{
	private final int id;
	private Integer subject;
	private Integer subjectAttributeType;
	private Integer subjectType;
	private Integer orderby;
	private String valueText;
	private Integer valueNumber;
	private Date valueDate;
	private Integer dataType;
	
	public SubjectAttribute(Integer id, Integer subject, Integer subjectAttributeType,
			Integer subjectType, Integer orderby, String valueText, Integer valueNumber,
			Date valueDate, Integer dataType) {
		super();
		this.id = id;
		this.subject = subject;
		this.subjectAttributeType = subjectAttributeType;
		this.subjectType = subjectType;
		this.orderby = orderby;
		this.valueText = valueText;
		this.valueNumber = valueNumber;
		this.valueDate = valueDate;
		this.dataType = dataType;
	}

	public Integer getSubject() {
		return subject;
	}

	public void setSubject(int subject) {
		this.subject = subject;
	}

	public int getSubjectAttributeType() {
		return subjectAttributeType;
	}

	public void setSubjectAttributeType(int subjectAttributeType) {
		this.subjectAttributeType = subjectAttributeType;
	}

	public int getSubjectType() {
		return subjectType;
	}

	public void setSubjectType(int subjectType) {
		this.subjectType = subjectType;
	}

	public int getOrderby() {
		return orderby;
	}

	public void setOrderby(int orderby) {
		this.orderby = orderby;
	}

	public String getValueText() {
		return valueText;
	}

	public void setValueText(String valueText) {
		this.valueText = valueText;
	}

	public int getValueNumber() {
		return valueNumber != null ? valueNumber: 0;
	}

	public void setValueNumber(int valueNumber) {
		this.valueNumber = valueNumber;
	}

	public Date getValueDate() {
		return valueDate;
	}

	public void setValueDate(Date valueDate) {
		this.valueDate = valueDate;
	}

	public Integer getDataType() {
		return dataType;
	}

	public void setDataType(int dataType) {
		this.dataType = dataType;
	}

	public int getId() {
		return id;
	}	
	
	public String getValue(){
		switch(getDataType()){
			case 1: return getValueText();
			case 2: return Integer.toString(getValueNumber());
			case 3: return getValueDate().toString();
		}
		return null;
	}
	
	/*OBJECT ACCESS METHODS*/
	
	public String getSubjectTypeString(){
		return SubjectType.getSubjectType(getSubjectType()).getType();
	}
		
	public Object getSubjectObject(){
		if(getSubjectType() == SubjectType.ENTERPRISE)
			return ImbiConnection.getEnterpriseDAO().find(getSubject());
		if(getSubjectType() == SubjectType.PERSON)
			return ImbiConnection.getPersonDAO().find(getSubject());
		return null;
	}
	
	public SubjectAttributeType getSubjectAttributeTypeObject(){
		return SubjectAttributeType.getSubjectAttributeType(getSubjectAttributeType());
	}

	@Override
	public int compareTo(SubjectAttribute other) {		
		return ((Integer)getOrderby()).compareTo(other.getOrderby());
	}
			
}
